package com.mytinyRPC.provider;


import com.mytinyRPC.exception.RpcError;
import com.mytinyRPC.exception.RpcException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/***
 * @Author HP
 * @Description //本地注册表
 **/
public class LocalRegisterImpl implements LocalRegister{

    private static final Logger logger = LoggerFactory.getLogger(LocalRegisterImpl.class);

    private static final Map<String,Class> serviceMap=new ConcurrentHashMap<>();
    private static final Set<String> registeredService = ConcurrentHashMap.newKeySet();

    public LocalRegisterImpl() {
    }

    /***
     * @Author HP
     * @Description //本地表注册
     * @Name regist
     * @Param [interfaceName, implClass]
     * @return void
     **/
    public void regist(String interfaceName,Class implClass){

        serviceMap.put(interfaceName,implClass);
        logger.info("向接口: {} 注册服务: {}", implClass.getClass().getInterfaces(),interfaceName);
    }
    /***
     * @Author HP
     * @Description //获取
     * @Name get
     * @Param [interfaceName]
     * @return java.lang.Class
     **/
    public Class get(String interfaceName){
        Class service = serviceMap.get(interfaceName);
        if (service == null) {
            throw new RpcException(RpcError.SERVICE_NOT_FOUND);
        }
        return service;
    }
}
